package com.stylefeng.guns.rest.modular.cinema;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.service.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.api.order.service.OrderServiceAPI;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaConditionResponseVO;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaFieldResponseVO;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaFieldsResponseVO;
import com.stylefeng.guns.rest.modular.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @创建人 lIUJF
 * @创建时间 2019/1/10
 * @描述:
 */


@Slf4j
@RestController
@RequestMapping("/cinema/")
public class CinemaController {
	private static final String IMG_PRE = "http://img.meetingshop.cn/";


	@Reference(interfaceClass = CinemaServiceAPI.class,connections = 10,check = false) //连接控制，不超过10
	private CinemaServiceAPI cinemaServiceAPI;

	@Reference(interfaceClass = OrderServiceAPI.class,check = false)
	private OrderServiceAPI orderServiceAPI;


	@RequestMapping(value = "getCinemas")
	public ResponseVO getCinemas(CinemaQueryVO cinemaQueryVO){
		try {
			//按照五个条件进行筛选
			Page<CinemaVO> cinemas = cinemaServiceAPI.getCinemas(cinemaQueryVO);
			//判断是否有满足条件的影院
			if (null != cinemas && cinemas.getRecords().size() > 0) {
				return ResponseVO.success(cinemas.getCurrent(), (int) cinemas.getPages(), null, cinemas.getRecords());
			} else {
				return ResponseVO.success("没有影院可查");
			}
		} catch (Exception e) {
			//如果出现异常，如何处理
			log.error("获取影院列表异常", e);
			e.printStackTrace();
		}
		return ResponseVO.success("aa");
	}

	//获取影院的查询条件。

	/**
	 * 1。热点数据 -》 放缓存
	 * 2。 banner
	 *
	 * @param cinemaQueryVO
	 * @return
	 */
	@RequestMapping(value = "getCondition")
	public ResponseVO getCondition(CinemaQueryVO cinemaQueryVO){
		//获取三个集合，封装成一个对象即可
		try {
			List<BrandVO> brands = cinemaServiceAPI.getBrands(cinemaQueryVO.getBrandId());
			List<AreaVO> areas = cinemaServiceAPI.getAreas(cinemaQueryVO.getDistrictId());
			List<HallTypeVO> hallTypes = cinemaServiceAPI.getHallTypes(cinemaQueryVO.getHallType());
			CinemaConditionResponseVO cinemaConditionResponseVO = new CinemaConditionResponseVO();
			cinemaConditionResponseVO.setAreaList(areas);
			cinemaConditionResponseVO.setBrandList(brands);
			cinemaConditionResponseVO.setHalltypeList(hallTypes);
			return ResponseVO.success(cinemaConditionResponseVO);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("获取影院查询条件失败", e);
			return ResponseVO.serviceFail("获取影院查询条件失败");
		}

	}

	@RequestMapping(value = "getFields")
	public ResponseVO getFields(Integer cinemaId){
		try {
			CinemaInfoVO cinemaInfoVO = cinemaServiceAPI.getCinemaInfoById(cinemaId);
			List<FilmInfoVO> filmInfoVOS = cinemaServiceAPI.getFilmInfoByCinemaId(cinemaId);
			CinemaFieldsResponseVO cinemaFieldResponseVO = new CinemaFieldsResponseVO();
			cinemaFieldResponseVO.setCinemaInfo(cinemaInfoVO);
			cinemaFieldResponseVO.setFilmList(filmInfoVOS);
			return ResponseVO.success(cinemaFieldResponseVO);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("获取播放场次失败", e);
			return ResponseVO.serviceFail("获取播放场次失败");
		}
	}

	@RequestMapping(value = "getFieldInfo", method = RequestMethod.POST)
	public ResponseVO getFieldInfo(Integer cineMaId, Integer fieldId){
		try {
			CinemaInfoVO cinemaInfoVO = cinemaServiceAPI.getCinemaInfoById(cineMaId);
			FilmInfoVO filmInfoVO = cinemaServiceAPI.getFilmInfoByFieldId(cineMaId);
			HallInfoVO hallInfoVO = cinemaServiceAPI.getFilmFieldInfo(fieldId);
			//造几个销售假数据，后续对接订单接口

			hallInfoVO.setSoldSeats(orderServiceAPI.getSoldSeatsByFieldId(fieldId));

			CinemaFieldResponseVO cinemaFieldResponseVO = new CinemaFieldResponseVO();
			cinemaFieldResponseVO.setCinemaInfo(cinemaInfoVO);
			cinemaFieldResponseVO.setFilmInfo(filmInfoVO);
			cinemaFieldResponseVO.setHallInfo(hallInfoVO);
			return ResponseVO.success(cinemaFieldResponseVO);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("获取播放场次失败", e);
			return ResponseVO.serviceFail("获取播放场次失败");
		}
	}
}
